package com.info.zhiduoduo.repository.mapper.address;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.address.TaxRateDto;
import com.info.zhiduoduo.repository.dto.taxfee.TaxFeeMoneyDto;
import com.info.zhiduoduo.repository.entity.address.AddressTaxRate;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

public interface AddressTaxRateMapper extends BaseMapper<AddressTaxRate> {

    /**
     * 根据userid得到地区id和状态查询年度税率或者季度税率
     */
    @Select("select zt.*,za.* from zdd_tax_rate zt INNER JOIN zdd_address_tax_rate za on zt.address_id=za.id " +
            " where #{money}>min_gear and #{money}<=max_gear and tax_type=#{taxtype} " +
            " and addid=(select opening_location from zdd_maker_account where userid=#{userid}) ")
    TaxRateDto getTaxationYearORQuarter(@Param("userid") Integer userid, @Param("money") BigDecimal money, @Param("taxtype") Integer taxtype);

    /**
     * 根据当前发票申请id查询：money：本次的金额，quarterMoney：季度总开票金额（以前的总金额），handMoney：已交税款
     */
    @Select(" select  " +
            " ifnull((select amount from zdd_taxer_invoice where invoiceid=#{invoiceid}), 0 ) as money, " +
            " ifnull( sum( ti.amount ), 0 ) as quarterMoney," +
            " ifnull( sum( ti.tax_fee ), 0 ) as handMoney " +
            " from zdd_taxer_invoice ti " +
            " INNER JOIN zdd_taxer_payment tp on ti.payid=tp.payid " +
            " where tp.state=2 and ti.userid =#{userid} " +
            " and YEAR(current_date)= YEAR(tp.confirmtime) " +
            " and quarter( current_date ) = quarter(tp.confirmtime) " +
            " and ti.deleted = 0")
    TaxFeeMoneyDto getquarterMoneys(@Param("invoiceid") Integer invoiceid,@Param("userid") Integer userid);

    /**
     * 根据当前发票申请id查询：money：本次的金额，yearMoney：年度总开票金额（以前的总金额），handMoney：已交税款
     */
    @Select(" select ifnull( SUM(ti.amount), 0 ) as yearMoney," +
            " ifnull( SUM(ti.tax_fee), 0 ) as handMoney, " +
            " ifnull((select amount from zdd_taxer_invoice where invoiceid=#{invoiceid}), 0 ) as money " +
            " from zdd_taxer_invoice ti " +
            " INNER JOIN zdd_taxer_payment tp on ti.payid=tp.payid " +
            " where tp.state=2 and ti.userid = #{userid} " +
            " and year(current_date)=year(tp.confirmtime) " +
            " and ti.deleted = 0")
    TaxFeeMoneyDto getYearMoneys(@Param("invoiceid") Integer invoiceid,@Param("userid") Integer userid);

    @Select("SELECT * FROM `zdd_address_tax_rate` where id=#{id} ")
    AddressTaxRate getAddressTaxRateByid(@Param("id") Integer id);

    @Update("update zdd_address_tax_rate set min_gear = #{max_gear} " +
            "where min_gear=#{min_gear} and addid=#{addid} ")
    Boolean updateAddressTaxRate(@Param("max_gear") Integer maxGear,@Param("min_gear") Integer minGear,@Param("addid") Integer addid);

    @Update("update zdd_address_tax_rate set max_gear = #{max_gear} " +
            "where id=#{id} ")
    Boolean updateAddressTaxRateById(@Param("max_gear") Integer maxGear, @Param("id") Integer id);

    @Update("update zdd_address_tax_rate set min_gear = #{min_gear} " +
            "where id=#{id} ")
    Boolean updateAddressTaxRateByIdAndMinGear(@Param("min_gear") Integer minGear, @Param("id") Integer id);

    @Update("update zdd_address_tax_rate set min_gear = #{min_gear},and_judge=#{and_judge} " +
            "where id=#{id} ")
    Boolean updateAddressTaxRateByIdAndJudge(@Param("min_gear") Integer minGear,@Param("and_judge") Integer andJudge, @Param("id") Integer id);
}
